﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bluegrams.Calcex;

namespace Parabola
{
    public class MathFunction
    {
        private Dictionary<double, double> data;
        private MathParser parser;

        public string Name { get; private set; }

        public string Term { get; private set; }

        public Color GraphColor { get; private set; }

        public MathFunction (string name, string term, Color col)
        {
            Name = name;
            Term = term;
            GraphColor = col;
            data = new Dictionary<double, double>();
            parser = new MathParser("x");
        }

        public Dictionary<double, double> GetData(int xstart, int xend)
        {
            Dictionary<double, double> result = new Dictionary<double, double>();
            if (data.Count == 0) return fillAll(xstart, xend);
            for (double i = xstart; i <= xend; i += 0.1)
            {
                i = Math.Round(i, 1);
                if (data.ContainsKey(i))
                {
                    result.Add(i, data[i]);
                }
                else
                {
                    parser.SetVariable("x", i);
                    double val = parser.Parse(Term).Evaluate();
                    result.Add(i, val);
                    data.Add(i, val);
                }
            }
            return result;
        }

        private Dictionary<double, double> fillAll(int xstart, int xend)
        {
            data = parser.Parse(Term).EvaluateList("x", xstart, (xend - xstart + 1) * 10, 1, 1);
            return data;
        }
    }
}
